package controllers

import (
	"BeegoFirst/models"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type TypeControllers struct {
	beego.Controller
}

func (This *TypeControllers) Get() {
	o := orm.NewOrm()
	q := o.QueryTable("article_type")
	var articleType []models.ArticleType
	_, err := q.All(&articleType)
	if err != nil {
		This.Ctx.WriteString("查询失败")
		return
	}

	This.Data["type"] = articleType
	This.Layout = "layout.html"

	This.TplName = "addType.html"
}

func (This *TypeControllers) Post() {
	title := This.GetString("typeName")
	if title == "" {
		This.Ctx.WriteString("不可以为空")
		return
	}

	articleType := models.ArticleType{Title: title}
	o := orm.NewOrm()
	_, err := o.Insert(&articleType)

	if err != nil {
		This.Ctx.WriteString("插入失败")
		return
	}

	This.Redirect("/addType", 302)

}
